package com.controller.lyxmcontroller.controller;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.tomcat.util.bcel.Const;
import org.springframework.stereotype.Controller;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.aliyun.oss.OSSClient;
import com.controller.lyxmcontroller.config.OSSClientConstants;
import com.controller.lyxmcontroller.util.AliyunOSSClientUtil;

@Controller
@RequestMapping("/ueditor")
public class UeditorController {

    @ResponseBody
    @RequestMapping("/admin/imgUpload2/config")
    public String exe(HttpServletRequest request,String action){
    	//读取配置文件
    	String a = this.ReadFile("E://lyxm//lyxm-controller//target//classes//static//ueditor//jsp//config.json");
        return a;
    }

    @ResponseBody
    @RequestMapping("/admin/imgUpload2/pictrueUpload1")
    public String exeaa(HttpServletRequest request,String action,HttpServletResponse response){
    	File tagetFile =null;
		System.out.println("upload");
		tagetFile = this.pictureUpload(request, response);
    	OSSClient ossClient = new OSSClient(OSSClientConstants.ENDPOINT, OSSClientConstants.ACCESS_KEY_ID, OSSClientConstants.ACCESS_KEY_SECRET);
        String name = AliyunOSSClientUtil.uploadObject2OSS(ossClient, tagetFile, OSSClientConstants.BACKET_NAME, OSSClientConstants.FOLDER);
        String url = OSSClientConstants.BACKET_NAME+"."+OSSClientConstants.ENDPOINT+"/"+OSSClientConstants.FOLDER+tagetFile.getName();
    	String config =
                "{\n" +
                "            \"state\": \"SUCCESS\",\n" +
                "                \"url\": \"https://"+url+"\",\n" +
                "                \"title\": \"path\",\n" +
                "                \"original\": \"path\"\n" +
                "        }";

    		    	
        return config;
    }
    public static void main(String[] args) {  
        // 读取nameID.txt文件中的NAMEID字段（key）对应值（value）并存储  
            UeditorController f = new UeditorController();
            String a = f.ReadFile("E://lyxm//lyxm-controller//target//classes//static//ueditor//jsp//config.json");
            System.out.println(a);
    }  
    
    public String ReadFile(String Path){
    	BufferedReader reader = null;
    	String laststr = "";
    	try{
    		FileInputStream fileInputStream = new FileInputStream(Path);
    		InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
    		reader = new BufferedReader(inputStreamReader);
    		String tempString = null;
    	while((tempString = reader.readLine()) != null){
    		laststr += tempString;
    	}
    		reader.close();
    	}catch(IOException e){
    		e.printStackTrace();
    	}finally{
    	
    		if(reader != null){
	    	try {
	    		reader.close();
		    	} catch (IOException e) {
		    		e.printStackTrace();
		    		}
	    		}
	    	}
    	return laststr;
	}
    
	public File pictureUpload(HttpServletRequest request,HttpServletResponse response){
		
        MultipartHttpServletRequest Murequest = (MultipartHttpServletRequest)request;
        Map<String, MultipartFile> files = Murequest.getFileMap();//得到文件map对象
        String path = ClassUtils.getDefaultClassLoader().getResource("").getPath();
        String targetpath = "static/img/linshi/";
        String targetpath1 = "img/linshi/";
        String  fileName ="";
        File wenjianjia = new File(path+targetpath);
        if(!wenjianjia.exists()){
        	wenjianjia.mkdir();
        }
        File tagetFile = null;
        for(MultipartFile file1 :files.values()){
            fileName=file1.getOriginalFilename();//相片的名字
            long times = new Date().getTime();
        	fileName = String.valueOf(times)+fileName;
            tagetFile = new File(path+targetpath+fileName);
            if(!tagetFile.exists()){//文件名不存在 则新建文件，并将文件复制到新建文件中
                try {
                    tagetFile.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                	file1.transferTo(tagetFile);//到这里就可以上传了，以下是处理图片及允许gif的上传
                	
                  //图片扩展名
                    String types=fileName.substring(fileName.lastIndexOf(".")+1).toLowerCase();
                } catch (IllegalStateException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }else{
            	 try {
                     tagetFile.createNewFile();
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
                 try {
                 	file1.transferTo(tagetFile);//到这里就可以上传了，以下是处理图片及允许gif的上传
                 	
                   //图片扩展名
                     String types=fileName.substring(fileName.lastIndexOf(".")+1).toLowerCase();
                 } catch (IllegalStateException e) {
                     e.printStackTrace();
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
            }
        }
        String a = targetpath1+fileName;
        
		return tagetFile;
	}
    
}

